package com.hsf.cloudweb.controller;

import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.hsf.cloudweb.config.AliYunMessageServiceConfig;
import com.hsf.cloudweb.model.ServiceResponse;
import com.hsf.cloudweb.model.vo.AliYunMessageInfo;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
public class SmsController extends BaseController{

    @Autowired
    private Gson gson;

  @GetMapping("getPhoneCode")
  public ServiceResponse<AliYunMessageInfo> getVerifyCode(@RequestParam("phoneNum") Long phoneNum){
      logger.info("phoneNum={}",phoneNum);
      ServiceResponse<AliYunMessageInfo> response = new ServiceResponse<>();
      String regionId = AliYunMessageServiceConfig.REGION_ID;
      String accessKeyId = AliYunMessageServiceConfig.ACCESS_KEY_ID;
      String accessKeySecret = AliYunMessageServiceConfig.ACCESS_KEY_SECRET;
      String domain = AliYunMessageServiceConfig.DOMAIN;
      String version = AliYunMessageServiceConfig.VERSION;
      String action = AliYunMessageServiceConfig.SEND_SMS_ACTION;
      String templateCode = AliYunMessageServiceConfig.SEND_SMS_TEMPLATE_CODE;
      String signName = AliYunMessageServiceConfig.SIGN_NAME;

      DefaultProfile profile = DefaultProfile.getProfile(regionId,accessKeyId,accessKeySecret);
      IAcsClient client = new DefaultAcsClient(profile);
      CommonRequest request = new CommonRequest();
      request.setMethod(MethodType.POST);

      request.setDomain(domain);
      request.setVersion(version);
      request.setAction(action);
      request.putQueryParameter("RegionId", regionId);
      request.putQueryParameter("PhoneNumbers",String.valueOf(phoneNum));
      request.putQueryParameter("SignName", signName);
      request.putQueryParameter("TemplateCode", templateCode);

      Character[] chars = new Character[]{'0','1','2','3','4','5','6','7','8','9'};
      StringBuilder codeBuilder = new StringBuilder();
      for(int i=0;i<6;i++){
          int index = new Random().nextInt(10);
          codeBuilder.append(chars[index]);
      }
      String code = codeBuilder.toString();
      request.putQueryParameter("TemplateParam", "{\"code\":\" "+code+"\"}");
      try {
          CommonResponse commonResponse = client.getCommonResponse(request);
          AliYunMessageInfo data = gson.fromJson(commonResponse.getData(),AliYunMessageInfo.class);
          response.setData(data);
          System.out.println(commonResponse.getData());
      } catch (ServerException e) {
          response.setStatus(500);
          response.setMessage("Inner Server Error");
          logger.error("Inner Server Error:"+e.getMessage());
      } catch (ClientException e) {
          response.setStatus(402);
          response.setMessage("Client Error:"+e.getErrMsg());
          logger.error("Client Error:"+e.getMessage());

      }
      return response;
  }

}
